<template>
  <div id="app">
    <h1>购物车</h1>
    <ul>
      <li v-for="(item, index) in items" :key="index">
        {{ item.name }} - ¥{{ item.price }} x {{ item.quantity }}
        <button @click="decrementQuantity(item.name)">-</button>
        <button @click="incrementQuantity(item.name)">+</button>
      </li>
    </ul>
    <div>总计: ¥{{ total }}</div>
  </div>
</template>
<script>
import { mapState, mapActions, mapGetters } from 'vuex';

export default {
  computed: {
    ...mapState(['items']),
    ...mapGetters(['total'])
  },
  methods: {
    ...mapActions(['addItem', 'removeItem', 'incrementQuantity', 'decrementQuantity'])
  }
}
</script>
<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

nav {
  padding: 30px;
}

nav a {
  font-weight: bold;
  color: #2c3e50;
}

nav a.router-link-exact-active {
  color: #42b983;
}
</style>
